//
//  FacetoCbmHttp.m
//  FaceTo
//
//  Created by chijia on 2017/7/15.
//  Copyright © 2017年 chengbangming. All rights reserved.
//

#import "FacetoCbmHttp.h"
#import "AFNetworking.h"
#import "FacetoParam.h"

@implementation FacetoCbmHttp
+ (void)get:(NSString *)url params:(NSDictionary *)params success:(void (^)(id responseObj))success failure:(void (^)(NSError *))failure
{
    // 1.获得请求管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // 2.添加请求头
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager.requestSerializer setValue:[FTUserDefault objectForKey:@"token"] forHTTPHeaderField:@"Authorization"];
    manager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript", nil];
    // 3.发送GET请求
    [manager GET:url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        // 这里可以获取到目前的数据请求的进度
        
    } success:^(NSURLSessionDataTask *task, id responseObj) {
        if (success) {
            success(responseObj);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}
+ (void)temporaryGet:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    // 1.获得请求管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // 2.添加请求头
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager.requestSerializer setValue:[FTUserDefault objectForKey:@"temporaryToken"] forHTTPHeaderField:@"Authorization"];
    manager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript", nil];
    // 3.发送GET请求
    [manager GET:url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        // 这里可以获取到目前的数据请求的进度
        
    } success:^(NSURLSessionDataTask *task, id responseObj) {
        if (success) {
            success(responseObj);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

+ (void)post:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    // 1.获得请求管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    //2.添加请求头
//    manager.requestSerializer = [AFJSONRequestSerializer serializer];//不允许接收到的json更改
//    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager.requestSerializer setValue:[FTUserDefault objectForKey:@"token"] forHTTPHeaderField:@"Authorization"];
    manager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
    manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingMutableContainers];//afn允许接受到的json可以更改

    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    securityPolicy.allowInvalidCertificates = YES;
    securityPolicy.validatesDomainName = NO;
    manager.securityPolicy = securityPolicy;
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/text", @"text/html",@"text/json",@"text/javascript",@"text/plain", nil];

    // 3.发送POST请求
    [manager POST:url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        // 这里可以获取到目前的数据请求的进度
        
    } success:^(NSURLSessionDataTask *task, id responseObj) {
        if (success) {
            success(responseObj);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

+ (void)imagePost:(NSString *)url params:(NSDictionary *)params data:(UIImage *)imagedata success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    // 1.获得请求管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // 2.添加请求头
    [manager.requestSerializer setValue:[FTUserDefault objectForKey:@"token"] forHTTPHeaderField:@"Authorization"];
    manager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    [manager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
        UIImage *uploadImage = imagedata;
        [formData appendPartWithFileData:UIImageJPEGRepresentation(uploadImage, 0.5f) name:@"avatar" fileName:@"test.jpg" mimeType:@"image/jpeg"];
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        // 这里可以获取到目前的数据请求的进度
    } success:^(NSURLSessionDataTask *task, id responseObj) {
        success(responseObj);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        failure(error);
    }];
}


+ (void)imagePost:(NSString *)url params:(NSDictionary *)params imageArray:(NSMutableArray *)imageArray success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    // 1.获得请求管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // 2.添加请求头
    [manager.requestSerializer setValue:[FTUserDefault objectForKey:@"token"] forHTTPHeaderField:@"Authorization"];
    manager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    [manager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        for (int i = 0; i<imageArray.count; i++) {
            UIImage *uploadImage = imageArray[i];
            [formData appendPartWithFileData:UIImageJPEGRepresentation(uploadImage, 1.0f) name:[NSString stringWithFormat:@"image%d",i+1] fileName:@"hzzj.jpg" mimeType:@"image/jpeg"];
        }
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        // 这里可以获取到目前的数据请求的进度
    } success:^(NSURLSessionDataTask *task, id responseObj) {
        success(responseObj);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        failure(error);
    }];
}
@end
